package com.xydtech.ecard.imp.service.impl;

import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.imp.domain.EcardApiMerchantImportTask;
import com.xydtech.ecard.imp.domain.EcardMerchantImport;
import com.xydtech.ecard.imp.domain.EcardMerchantImportHandover;
import com.xydtech.ecard.imp.domain.EcardMerchantImportTask;
import com.xydtech.ecard.imp.mapper.EcardApiMerchantImportTaskMapper;
import com.xydtech.ecard.imp.mapper.EcardMerchantImportHandoverMapper;
import com.xydtech.ecard.imp.mapper.EcardMerchantImportMapper;
import com.xydtech.ecard.imp.mapper.EcardMerchantImportTaskMapper;
import com.xydtech.ecard.imp.service.IEcardApiMerchantImportTaskService;
import com.xydtech.ecard.user.domain.EcardApiEcardRegistInfo;
import com.xydtech.ecard.user.service.IEcardApiEcardRegistInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/*
 * 电子名片导入任务service层
 * @author lin
 * @version 1.0
 * @create 2024/11/28 9:08
 */
@Service
public class EcardApiMerchantImportTaskServiceImpl implements IEcardApiMerchantImportTaskService {

    @Autowired
    private EcardMerchantImportMapper ecardMerchantImportMapper;
    @Autowired
    private EcardApiMerchantImportTaskMapper ecardApiMerchantImportTaskMapper;
    @Autowired
    private EcardMerchantImportHandoverMapper ecardMerchantImportHandoverMapper;
    @Autowired
    private EcardMerchantImportTaskMapper ecardMerchantImportTaskMapper;
    @Autowired
    private IEcardApiEcardRegistInfoService ecardApiEcardRegistInfoService;

    /**
     * 查询导入任务（批量任务）
     *
     * @param cmRegistId
     * @param merchantType
     * @param search
     * @param deptId
     * @param phone
     * @param salt
     * @return
     */
    @Override
    public List<EcardApiMerchantImportTask> getMerchantImportTaskList(Long cmRegistId, String merchantType, String search, Long deptId, String phone, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(phone)) {
                phone = (AESEncryptionUtils.encrypt(phone, salt));
            }

            List<EcardApiMerchantImportTask> merchantImportTaskList = ecardApiMerchantImportTaskMapper.getMerchantImportTaskList(cmRegistId, merchantType, search, deptId, phone);
            for (EcardApiMerchantImportTask ecardApiMerchantImportTask : merchantImportTaskList) {
                if (!StringUtils.objIsNullOrEmpty(ecardApiMerchantImportTask.getPhoneNumber())) {
                    ecardApiMerchantImportTask.setPhoneNumber(AESEncryptionUtils.decrypt(ecardApiMerchantImportTask.getPhoneNumber(), salt));
                }
            }
            return merchantImportTaskList;

        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    /**
     * 转交并保存转交记录
     *
     * @param ecardMerchantImportHandover
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addMerchantImportTransferInfo(EcardMerchantImportHandover ecardMerchantImportHandover, String salt) {
        //修改导入任务归属人
        EcardMerchantImportTask ecardMerchantImportTask = ecardMerchantImportTaskMapper.selectEcardMerchantImportTaskByTaskId(ecardMerchantImportHandover.getTaskId());
        if (ecardMerchantImportTask == null) {
            return -1;
        }
        //保存转交记录
        ecardMerchantImportHandover.setImportId(ecardMerchantImportTask.getImportId());
        ecardMerchantImportHandoverMapper.insertEcardMerchantImportHandover(ecardMerchantImportHandover);

        //修改导入任务归属人
        //接收人注册id
        ecardMerchantImportTask.setCmRegistId(ecardMerchantImportHandover.getRecipientId());
        //接收人所在机构
        ecardMerchantImportTask.setDeptId(ecardMerchantImportHandover.getRecipientDeptId());
        ecardMerchantImportTaskMapper.updateEcardMerchantImportTask(ecardMerchantImportTask);

        //修改导入数据归属人
        EcardApiEcardRegistInfo ecardRegistInfo = ecardApiEcardRegistInfoService.selectEcardApiEcardRegistInfoByRegistId(ecardMerchantImportHandover.getRecipientId(), salt);
        EcardMerchantImport ecardMerchantImport = new EcardMerchantImport();
        ecardMerchantImport.setId(ecardMerchantImportTask.getImportId());
        ecardMerchantImport.setUserCode(ecardRegistInfo.getUserCode());
        ecardMerchantImport.setDeptId(ecardMerchantImportHandover.getRecipientDeptId());
        return ecardMerchantImportMapper.updateEcardMerchantImport(ecardMerchantImport);
    }

    /**
     * 关闭个体工商户导入任务
     *
     * @param taskId
     * @return
     */
    @Override
    public int closeMerchantImportTask(Long taskId) {
        EcardMerchantImportTask ecardMerchantImportTask = ecardMerchantImportTaskMapper.selectEcardMerchantImportTaskByTaskId(taskId);
        EcardMerchantImport ecardMerchantImport = new EcardMerchantImport();
        ecardMerchantImport.setId(ecardMerchantImportTask.getImportId());
        ecardMerchantImport.setState(0);
        ecardMerchantImportMapper.updateEcardMerchantImport(ecardMerchantImport);

        ecardMerchantImportTask.setSts("1");
        ecardMerchantImportTask.setCloseTime(new Date());
        return ecardMerchantImportTaskMapper.updateEcardMerchantImportTask(ecardMerchantImportTask);
    }
}
